
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:get/get_state_manager/src/simple/get_state.dart';
import 'package:get/get_state_manager/src/simple/get_view.dart';
import 'package:yt_devops/components/gaps.dart';

import 'package:yt_devops/components/yt_text_style.dart';
import 'package:yt_devops/model/device_obj/properties_model.dart';
import 'package:yt_devops/pages/devices/device_tab/device_detail/new_air_cooling_page/device_base_info_widget/device_base_info_widget_controller.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/func_utils.dart';

/**
 *
 * @Description TODO
 * @Author liuer
 * @CreateTime 2024年01月18日 15:04
 */
class BaseInfoDynamicTabView extends GetView {
  final List list;

  const BaseInfoDynamicTabView(this.list, {super.key});

  @override
  Widget build(BuildContext context) {
    // Log.d('context===' + list.toString());
    return GetBuilder<DeviceBaseInfoWidgetController>(
        builder: (_) =>  ListView.separated(
          separatorBuilder: (BuildContext context, int index) =>
              Divider(
                height: 1,
                endIndent: 16.w,
                indent: 16.w,
              ),
          padding: EdgeInsets.zero,
          itemBuilder: (BuildContext context, int index) {
            return _itemWidget(index);
          },
          itemCount: list.length,
        ));
  }

  _itemWidget(int index) {
    Map<String,dynamic> map = list[index];
    String title = map['name'].toString();
    String value = map['value'].toString();

    PropertiesModel model = PropertiesModel.fromJson(map);
    var realValue = FuncUtils.formatModelValue(
        value, model.dataType,isShowUnit: false).toString().trim();
    if (realValue.isNotEmpty && realValue != '--' &&
        map['dataType'] != null &&
        map['dataType']['specs'] != null &&
        map['dataType']['specs']['unit'] != null &&
        map['dataType']['specs']['unit'].toString().isNotEmpty) {
      realValue = '$realValue(${map['dataType']['specs']['unit']})';
    }
    Color valueColor = Colour.mainTextColor;
    if(realValue == ResKey.onLine.tr){
      valueColor = Colour.green;
    }else if(realValue == ResKey.onLine.tr){
      valueColor = Colour.green;
    }else if(realValue == ResKey.alarm.tr || realValue == ResKey.alarm.tr){
      valueColor = Colour.warningTextColor;
    }else if(realValue == ResKey.offLine.tr){
      valueColor = Colour.labelTextColor;
    }
    // Log.d('title===' + title.toString());
    return
      title == "margin"
          ? //如果title是margin则说明需要加分割线,为了美观
      Container(
        height: 10,
        color: Colour.backgroundColor,
      )
          :
      Container(
        alignment: Alignment.center,
        height: 50,
        color: Colors.white,
        child: Row(
          children: [
            Gaps.hGap16,
            Text(
              title,
              style: ytTextStyle(fontSize: 14, color: Colour.otherTextColor),
            ),
            const Spacer(),
            Text(
              realValue,
              style: ytTextStyle(fontSize: 14,color:valueColor ),
            ),
            Gaps.hGap16,
          ],
        ),
      );
  }
}
